package com.jiang.spring.service.impl;

import com.jiang.spring.dao.BookDao;
import com.jiang.spring.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class BookServiceImpl implements BookService {
    
    @Autowired
    private BookDao bookDao;
    
    @Override
    @Transactional(
          //  readOnly = true 只读
          //  timeout = 3 超时回滚，
          //  rollbackFor =      默认运行时异常回滚
          //  noRollbackFor =    不因什么而回滚
            // isolation = Isolation.DEFAULT  隔离级别
    )
    public void buyBook(Integer userId,Integer bookId){
        //查询图书价格
        Integer price = bookDao.getPriceByBookId(bookId);
        //更新图书库存
        bookDao.updateStock(bookId);
        //更新用户余额
        bookDao.updateBalance(userId,price);
    }
}
